using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UniversalAdmin.Api.Models;
using UniversalAdmin.Application.Common;
using UniversalAdmin.Application.DTOs;
using UniversalAdmin.Application.Features.Roles.Commands.CreateRole;
using UniversalAdmin.Application.Features.Roles.Commands.DeleteRole;
using UniversalAdmin.Application.Features.Roles.Queries.GetPagedRoles;
using UniversalAdmin.Application.Features.Roles.Queries.GetRoleDetail;
using UniversalAdmin.Application.Features.Roles.Queries.GetRolesByName;

namespace UniversalAdmin.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
[Authorize]
public class RolesController : ControllerBase
{
  private readonly IMediator _mediator;

  public RolesController(IMediator mediator) => _mediator = mediator;

  [HttpGet]
  [Authorize("role.view")]
  public async Task<IActionResult> GetRoles(
      [FromQuery] int page = 1,
      [FromQuery] int size = 10,
      [FromQuery] string? keyword = null,
      [FromQuery] bool? isActive = null)
  {
    var result = await _mediator.Send(new GetPagedRolesQuery(
        PageIndex: page, 
        PageSize: size, 
        Keyword: keyword, 
        IsActive: isActive));
    return Ok(ApiResponse<PagedResult<RoleDto>>.Success(result.Data!, "获取成功"));
  }

  [HttpGet("{id:int}")]
  [Authorize("role.view")]
  public async Task<IActionResult> GetRole(int id)
  {
    var result = await _mediator.Send(new GetRoleDetailQuery(id));
    return result.Ok
        ? Ok(ApiResponse<RoleDto>.Success(result.Data!, "获取成功"))
        : NotFound(ApiResponse.Error(404, result.Message));
  }

  // 模糊查询角色名称
  [HttpGet("search")]
  public async Task<IActionResult> SearchRoles(
      [FromQuery] string? name,
      [FromQuery] int pageIndex = 1,
      [FromQuery] int pageSize = 10)
  {
    var query = new GetRolesByNameQuery
    {
      Name = name,
      PageIndex = pageIndex,
      PageSize = pageSize
    };

    var result = await _mediator.Send(query);
    return result.Ok ? Ok(result) : BadRequest(result);
  }

  [HttpPost]
  [Authorize("role.create")]
  public async Task<IActionResult> CreateRole([FromBody] CreateRoleCommand cmd)
  {
    if (!ModelState.IsValid) return BadRequest(ApiResponse.Error(400, "参数错误"));
    var result = await _mediator.Send(cmd);
    return result.Ok
        ? CreatedAtAction(nameof(GetRole), new { id = result.Data }, ApiResponse<int>.Success(result.Data!, "创建成功"))
        : BadRequest(ApiResponse.Error(result.Code, result.Message));
  }

  [HttpPut("{id:int}")]
  [Authorize("role.update")]
  public async Task<IActionResult> UpdateRole(int id, [FromBody] UpdateRoleCommand cmd)
  {
    if (id != cmd.Id) return BadRequest(ApiResponse.Error(400, "ID 不匹配"));
    var result = await _mediator.Send(cmd);
    return result.Ok
        ? Ok(ApiResponse<int>.Success(result.Data!, "更新成功"))
        : BadRequest(ApiResponse.Error(result.Code, result.Message));
  }

  [HttpDelete("{id:int}")]
  [Authorize("role.delete")]
  public async Task<IActionResult> DeleteRole(int id)
  {
    var result = await _mediator.Send(new DeleteRoleCommand(id));
    return result.Ok
        ? Ok(ApiResponse.Success("删除成功"))
        : BadRequest(ApiResponse.Error(result.Code, result.Message));
  }
}